<template>
  <div>
    <div>【服务资源占用比】</div>
    <div ref="verticalRef" class="w-full h-full"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

const verticalRef = ref(null)
let myChart: any = null
onMounted(() => {
  myChart = echarts.init(verticalRef.value)
  renderChart()
})

const renderChart = () => {
  let option = {
    xAxis: {
      type: "category",
      data: props.data.servers.map((item: any) => item.name),
      // axisLine: {
      //   show: false
      // },
      axisTick: {
        show: false
      },
      axisLabel: {
        show: true,
        color: '#9EB1C8'
      }
    },
    yAxis: {
      show: false,
      type: "value",
      max: function(value: any) {
        return Math.floor(value.max * 1.2)
      }
    },
    grid: {
      top: 16,
      right: 0,
      bottom: 26,
      left: -26,
      containLabel: true
    },
    series: [
      {
        type: 'bar',
        showBackground: false,
        data: props.data.servers.map((item: any) => {
          return {
            name: item.name,
            value: item.value
          }
        }),
        itemStyle: {
          color: '#479AD3',
          borderRadius: 5,
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          shadowBlur: 5
        },
        barWidth: 12,
        label: {
          show: true,
          position: 'top',
          color: '#fff',
          formatter: '{c}%'
        }
      }
    ]

  }
  myChart.setOption(option)
}

watch(() => props.data, () => {
  renderChart()
})

</script>

<style scoped></style>
